package com.jiyici.view;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.jiyici.R;
import com.jiyici.utils.StringUtils;

public class DialogWrapper {

    public static enum DialogMode {
        DoneAndCancel, OkOnly
    };

    public static interface DialogAction {
        public static final int NOOP = 0;
        public static final int DISMISS = 1;

        int run();
    }

    private Context mContext;

    private Dialog mDialog;

    private View mContentView;
    private TextView mBtnDone;
    private boolean mBtnDoneInitailDisabled;

    private DialogMode mMode;

    private String mTitleText;
    private int mDoneBtnTextId;
    private int mCancelBtnTextId;
    private DialogAction mDoneAction;
    private DialogAction mCancelAction;

    private int mOkBtnTextId;
    private DialogAction mOkAction;

    private View mDialogView;

    public DialogWrapper(Context context) {
        super();
        this.mContext = context;
    }

    public View getContentView() {
        return mContentView;
    }

    public void setContentView(int layoutResId) {
        mDialog = new Dialog(mContext, R.style.dialog);
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        mDialogView = layoutInflater.inflate(R.layout.dialog_base, null);
        ViewGroup contentViewLayout = (ViewGroup) mDialogView.findViewById(R.id.dialog_body);
        mContentView = layoutInflater.inflate(layoutResId, contentViewLayout);
    }
    
    public void setTitle(int titleTextId) {
        mTitleText = mContext.getString(titleTextId);
    }
    
    public void setTitle(String title) {
        mTitleText = title;
    }

    public void setAction(int okTextId, DialogAction action) {
        mMode = DialogMode.OkOnly;
        mOkBtnTextId = okTextId;
        mOkAction = action;
    }

    public void setAction(int doneTextId, DialogAction doneAction, int cancelTextId, DialogAction cancelAction) {
        mMode = DialogMode.DoneAndCancel;
        mDoneBtnTextId = doneTextId;
        mDoneAction = doneAction;
        mCancelBtnTextId = cancelTextId;
        mCancelAction = cancelAction;
    }

    public View getDoneButton() {
        return mBtnDone;
    }

    public void setBtnDoneInitailDisabled(boolean disable) {
        this.mBtnDoneInitailDisabled = disable;
    }

    public void show() {
        TextView dialogTitle = (TextView) mDialogView.findViewById(R.id.dialog_title);
        if (StringUtils.isEmpty(mTitleText)) {
            dialogTitle.setVisibility(View.GONE);
        } else {
            dialogTitle.setText(mTitleText);
        }
        View done_cancel_btns = mDialogView.findViewById(R.id.done_cancel_btns);
        TextView btn_ok = (TextView) mDialogView.findViewById(R.id.btn_ok);

        View.OnClickListener dialogBtnOnClickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (R.id.btn_done == v.getId()) {
                    if (mDoneAction != null) {
                        if (mDoneAction.run() == DialogAction.DISMISS) {
                            mDialog.dismiss();
                        }
                    }
                } else if (R.id.btn_cannel == v.getId()) {
                    if (mCancelAction != null)
                        mCancelAction.run();
                    mDialog.dismiss();
                } else if (R.id.btn_ok == v.getId()) {
                    if (mOkAction != null)
                        mOkAction.run();
                    mDialog.dismiss();
                }
            }
        };
        if (mMode == DialogMode.DoneAndCancel) {
            done_cancel_btns.setVisibility(View.VISIBLE);
            btn_ok.setVisibility(View.GONE);
            TextView btnCancel = (TextView) done_cancel_btns.findViewById(R.id.btn_cannel);
            mBtnDone = (TextView) done_cancel_btns.findViewById(R.id.btn_done);
            btnCancel.setText(mCancelBtnTextId);
            mBtnDone.setText(mDoneBtnTextId);
            mBtnDone.setOnClickListener(dialogBtnOnClickListener);
            btnCancel.setOnClickListener(dialogBtnOnClickListener);

            if (mBtnDoneInitailDisabled) {
                mBtnDone.setEnabled(false);
            }
        } else if (mMode == DialogMode.OkOnly) {
            done_cancel_btns.setVisibility(View.GONE);
            btn_ok.setVisibility(View.VISIBLE);
            btn_ok.setText(mOkBtnTextId);
            btn_ok.setOnClickListener(dialogBtnOnClickListener);
        }

        int widthPX = mContext.getResources().getDimensionPixelSize(R.dimen.dialog_width);
        android.view.ViewGroup.LayoutParams lp = new LayoutParams(widthPX, LayoutParams.WRAP_CONTENT);
        mDialog.setContentView(mDialogView, lp);
        mDialog.show();
    }

    public void dismiss() {
        mDialog.dismiss();
    }
}